package com.kssoft.ebd.common;

import lombok.Data;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@Data
public class Broadcast implements Serializable, Cloneable {
	private static final long serialVersionUID = 1L;
/**
 *  本地平台广播记录
 */
private String callRecordId;
    /**
     * 消息ID
     */
    private String id;

    /**
     * 请求广播源资源地址
     */
	private String source;

    /**
     *  广播目标地址(以区域码方式提供)
     * 	@Convert(converter = TargetsConverter.class)
     */
	private String[] targets;


    /**
     * 广播类型
     */
	private Type type;


    /**
     * 事件等级
     */

	private EventLevel eventLevel;

    /**
     * 事件类型
     */
	private String eventType;

    /**
     * 音量
     */
	private Integer volume;

    /**
     * 开始时间
     */
    private Timestamp beginTime;

    /**
     * 持续时间(秒)
     */
    private int duration;
    private int loop;

    private String title;



    /**
     * 广播发起者信息
     */
	private Timestamp createTime;
	private Integer creatorId;
	private String creatorName;
	private String organName;
	private String organCode;
	private String regionName;
	private String regionCode;

	private String extendId;


    /**
     * 定时广播id
     */
    private String timerTaskId;

    /**
     * 广播发布情况 // 广播发布状态
     */
	private Status status;

	/**
	 *  审核状态
	 */

	private ReviewStatus reviewStatus;


	/**
	 *  广播源
	 */

	private BroadSourceTypeEnum broadSource;



    /**
     * 结束时间
     */
	private Timestamp endTime;



	private From from = From.OTHER;


    private SendTo sendTo = SendTo.DEFAULT;


	public String getRegionCode() {
		if(regionCode == null && source != null) {
			switch (Integer.parseInt(source.substring(0, 1))) {
			case 2: regionCode = source.substring(1, 3); break;
			case 3: regionCode = source.substring(1, 5); break;
			case 4: regionCode = source.substring(1, 7); break;
				case 5:
					regionCode = source.substring(1, 10);
					break;
				default:
					regionCode = source.substring(1, 13);
					break;
			}
		}
		return regionCode;
	}


	public From getFrom() {// type
		if (type == null)
			return From.OTHER;

		if (Type.RELEASE_MANOEUVRE.equals(type) ||
				Type.SIMULATE_MANOEUVRE.equals(type) ||
				Type.ACTUAL_MANOEUVRE.equals(type)) {
			return From.MANOEUVRE;
		}
		return from;
	}



	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Broadcast) {
			Broadcast v = (Broadcast) obj;
			return v.getId().equals(getId()) && v.getTargets().equals(getTargets());
		}

		return false;
	}

	public enum Type {
        /**
         * 1：应急演练-系统演练；2：应急演练-模拟演练；3：应急演练-实际演练；4：应急广播；5：日常广播。其他：预留
         *
         */
		NONE, RELEASE_MANOEUVRE, SIMULATE_MANOEUVRE, ACTUAL_MANOEUVRE, EMERGENT, DAILY;

		public static Type valueOf(int value) {
			switch (value) {
			case 1:
				return RELEASE_MANOEUVRE;
			case 2:
				return SIMULATE_MANOEUVRE;
			case 3:
				return ACTUAL_MANOEUVRE;
			case 4:
				return EMERGENT;
			case 5:
				return DAILY;
			default:
				return NONE;
			}
		}

		public boolean gt(Type type) {
			return ordinal() > type.ordinal();
		}

		public boolean lt(Type type) {
			return ordinal() < type.ordinal();
		}
	}



    /**
     * 应急广播 消息级别
     *
     * @author sparrow
     */
    public enum EventLevel {
        NONE, MAJOR, GREAT, LARGER, DAILY;

        public static EventLevel valueOf(int value) {
            switch (value) {
                case 0:
                    return NONE;
                case 1:
                    return MAJOR;
                case 2:
                    return GREAT;
                case 3:
                    return LARGER;
                case 4:
                    return DAILY;
                default:
                    return DAILY;
            }
        }

        public boolean gt(EventLevel eventLevel) {
            return ordinal() > eventLevel.ordinal();
        }

        public boolean lt(EventLevel eventLevel) {
            return ordinal() < eventLevel.ordinal();
        }
    }

	// 广播状态 0-添加 1-播放中 2-播放结束 3-失败 4-过期
	public enum Status {
		NEW, PLAYING, PLAYED, FAILURE, OUT, CANCEL
	}

	// 审核状态 0-待审核 1-审核通过 2-审核失败
	public enum ReviewStatus {
		WAIT, REVIEW, FAILED
	}

	public enum From {
		OTHER,			// 其它
		PLATFORM,		// 平台
		EQUIPMENT,		// 设备
		LINKAGE,		// 联动协议
		MOBILE_PLATFORM,// 移动平台
		TIMER_TASK,		// 定时任务(不在状态栏显示)
		MANOEUVRE,		// 演练(不在状态栏显示)
		CAP_PLATFORM	// 广东预警平台
	}

	public enum SendTo {
	    DEFAULT,
        SUPPER_PLATFORM;

        public static SendTo valueOf(int value){
            switch (value){
                case 1:
                    return SUPPER_PLATFORM;
                default:
                    return DEFAULT;
            }
        }
    }

    public enum TaskState {
        /**
         * 0: 未处理； 1：等待播发； 2：播发中； 3：播发成功； 4：播发失败； 5：播发取消
         *
         */
        UNTREATED, WAITING, BROADCASTING, BROADCAST_SUCCEED, BROADCAST_FAILURE, BROADCAST_CANCEL;

        public static TaskState valueOf(int value) {
            switch (value) {
                case 0:
                    return UNTREATED;
                case 1:
                    return WAITING;
                case 2:
                    return BROADCASTING;
                case 3:
                    return BROADCAST_SUCCEED;
                case 4:
                    return BROADCAST_FAILURE;
                case 5:
                    return BROADCAST_CANCEL;
                default:
                    return UNTREATED;
            }
        }
    }

	public enum BroadSourceTypeEnum {
		EBM_BROAD(0, "上级联动广播"),
		EBI_BROAD(1, "前置联动广播"),
		AMQ_PROAD(2, "AMQP申请广播"),
		LOCAL_BROAD(3, "本级广播"),
		API_BROAD(4, "接口调度广播"),
		LED_BROAD(5, "led调度广播"),
		AUTO_REVIEW(7, "预制自动审核");

		private final int value;
		private final String description;

		BroadSourceTypeEnum(int value, String description) {
			this.value = value;
			this.description = description;
		}

		public static BroadSourceTypeEnum valueOf(int value) {
			for (BroadSourceTypeEnum source : values()) {
				if (source.value == value) {
					return source;
				}
			}
			return null;
		}

		public int getValue() {
			return value;
		}

		public String getDescription() {
			return description;
		}

		public static Map<Integer, String> getAllValuesAndDescriptions() {
			Map<Integer, String> valuesAndDescriptions = new HashMap<>();
			for (BroadSourceTypeEnum source : values()) {
				if (source != BroadSourceTypeEnum.AUTO_REVIEW) {
					valuesAndDescriptions.put(source.value, source.description);
				}
			}
			return valuesAndDescriptions;
		}

	}

}